package system

import (
	"MoSkeleton/framework/core/models"
	"MoSkeleton/models/base"
	"database/sql"
	"github.com/samber/lo"
	"gorm.io/gorm"
)

type SysDictType struct {
	models.MoModel
	DictName  string         `gorm:"column:dict_name;type:VARCHAR(100);"`
	DictType  string         `gorm:"column:dict_type;type:VARCHAR(100);"`
	Status    string         `gorm:"column:status;type:CHAR(1);"`
	CreatedBy string         `gorm:"column:created_by;type:VARCHAR(64);"`
	UpdatedBy string         `gorm:"column:updated_by;type:VARCHAR(32);"`
	Remark    sql.NullString `gorm:"column:remark;type:VARCHAR(500);"`
}

func (d *SysDictType) TableName() string {
	return "sys_dict_type"
}

func (d *SysDictType) SelectById(tx *gorm.DB, id uint64) (*SysDictType, error) {
	var dictType SysDictType
	err := base.GetModelDB(tx).Where("id = ?", id).First(&dictType).Error
	return &dictType, err
}

func (d *SysDictType) UpdateDictType(tx *gorm.DB) (int64, error) {
	result := base.GetModelDB(tx).Updates(d)
	return result.RowsAffected, result.Error
}

func (d *SysDictType) GetDictTypesByIds(tx *gorm.DB, ids *[]uint64) (*[]string, error) {
	var dictTypes []*SysDictType
	err := base.GetModelDB(tx).Select("dict_type").Where("id in (?)", *ids).Find(&dictTypes).Error
	if err != nil {
		return nil, err
	}
	dictTypeStrs := lo.Map(dictTypes, func(item *SysDictType, index int) string {
		return item.DictType
	})
	return &dictTypeStrs, nil
}
